package com.sojson.config.qps.sentinel.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.sojson.annotation.aspect.PointcutsClient;
import com.sojson.annotation.factory.SentinelResourceFactory;
import com.sojson.config.qps.sentinel.util.SentinelUtil;

/**
 * 自定义限流切面(拦截全部资源)
 * 
 * @author liu
 * @date 2021-08-25
 */
@Order(1)
@Aspect
@Component
public class CustomSentinelAspectSupport extends ASentinelAspectSupport {

    private SentinelResource srAll = new SentinelResourceFactory("all").build();
    private SentinelResource srRequest = new SentinelResourceFactory("request").build();

    /** 切点 */
    @Pointcut(PointcutsClient.POINT_CONTROLLER)
    public void all() {}

    /** 环绕通知 */
    // @Around("all()")
    public Object all(ProceedingJoinPoint pjp) throws Throwable {
        return SentinelUtil.invokeResourceWithSentinel(pjp, this, srAll);
    }

    /** 切点 */
    @Pointcut(PointcutsClient.POINTCUT_MAPPING_REQUEST)
    public void request() {}

    /** 环绕通知 */
    @Around("request()")
    public Object request(ProceedingJoinPoint pjp) throws Throwable {
        return SentinelUtil.invokeResourceWithSentinel(pjp, this, srRequest);
    }

}